﻿using LoveNote.Database.Entities;
using LoveNote.Database.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TookKit.Mvvm.Attributes;

namespace LoveNote.Database.Sqlite.Repositories
{
    [DependencyInjection(TookKit.Mvvm.Enums.Lifetime.Singleton, typeof(IUserRepository))]
    class UserRepository : RepositoryBase, IUserRepository
    {
        public int Delete(UserEntity entity)
        {
            if (entity == null)
            {
                return 0; 
            }
            db.Entry(entity).State = Microsoft.EntityFrameworkCore.EntityState.Deleted;
            return db.SaveChanges();
        }

        public List<UserEntity> GetAll()
        {
            return db.Users.ToList();
        }

        public int Insert(UserEntity entity)
        {
            db.Users.Add(entity);
            return db.SaveChanges();
        }

        public UserEntity Search(string username)
        {
            ArgumentNullException.ThrowIfNull(username, nameof(username));
            return db.Users.Where(t => t.UserName == username).FirstOrDefault();
        }

        public int Update(UserEntity entity)
        {
            db.Entry(entity).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
            return db.SaveChanges();
        }
    }
}
